﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Maticsoft.Common;

namespace Maticsoft.Web.Admin.Info
{
    public partial class EditNews : PageBase
    {
        BLL.News bll = new BLL.News();

        string id = string.Empty;
        string act = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            Check("content", "modify");

            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                id = Request.QueryString["id"];
            }
            if (!string.IsNullOrEmpty(Request.QueryString["act"]))
            {
                act = Request.QueryString["act"];
            }

            if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(act))
            {
                if (!IsPostBack)
                {
                    GetData();
                }
            }
        }

        protected void GetData()
        {
            Model.News model = bll.GetModel(int.Parse(id));
            txtTitle.Text = model.Title;
            FCKeditor1.Value = model.Detail;
            txtOrderNo.Text = model.OrderNo.ToString();
            ddlType.SelectedValue = model.ChannelId.ToString();
            cbTop.Checked = model.IsTop == 1 ? true : false;
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (act == "add")
            {
                Model.News model = new Model.News();
                model.Title = txtTitle.Text.Trim();
                model.ChannelId = int.Parse(ddlType.SelectedValue);
                model.Detail = FCKeditor1.Value;
                model.Hits = 0;
                model.OrderNo = int.Parse(txtOrderNo.Text.Trim());
                model.CreateTime = DateTime.Now;
                model.UpdateTime = DateTime.Now;
                model.IsActive = 1;
                model.IsTop = cbTop.Checked == true ? 1 : 0;

                bll.Add(model);

                MessageBox.ShowAndRedirect(this.Page, "添加成功", "News.aspx");
            }
            if (act == "edit")
            {
                Model.News model = bll.GetModel(int.Parse(id));
                model.Title = txtTitle.Text.Trim();
                model.ChannelId = int.Parse(ddlType.SelectedValue);
                model.Detail = FCKeditor1.Value;
                model.OrderNo = int.Parse(txtOrderNo.Text.Trim());
                model.UpdateTime = DateTime.Now;
                model.IsActive = 1;
                model.IsTop = cbTop.Checked == true ? 1 : 0;

                bll.Update(model);

                MessageBox.ShowAndRedirect(this.Page, "修改成功", "News.aspx");
            }
        }
    }
}